import 'package:bruno/bruno.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import '../utils/util.dart';
import './route/router_config.dart' as prefix;

class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  DateTime? lastTime;
  late ScreenUtilInit utilInit;
  @override
  void initState() {
    super.initState();

    buildContent(context);

    // MethodChannelManager.initChannel();
  }

  void buildContent(BuildContext context) {
    utilInit = ScreenUtilInit(
      designSize: const Size(375, 812),
      minTextAdapt: true,
      splitScreenMode: true,
      builder: (BuildContext context, widget) => WillPopScope(
        child: GetMaterialApp(
          theme: tpThemeData,
          enableLog: kDebugMode,
          title: '金科招引',
          builder: (ctx, child) {
            ScreenUtil.init(ctx);
            final easyLoading = EasyLoading.init();
            child = MediaQuery(
              //设置文字大小不随系统设置改变
              data: MediaQuery.of(ctx).copyWith(textScaleFactor: 1.0),
              child: GestureDetector(
                onTap: () {
                  FocusScopeNode focus = FocusScope.of(context);
                  if (!focus.hasPrimaryFocus && focus.focusedChild != null) {
                    FocusManager.instance.primaryFocus?.unfocus();
                  }
                },
                child: easyLoading(context, child),
                // ),
              ),
            );

            return child;
          },
          debugShowCheckedModeBanner: false,
          localizationsDelegates: const [
            GlobalMaterialLocalizations.delegate,
            GlobalWidgetsLocalizations.delegate,
            GlobalCupertinoLocalizations.delegate, //iOS
          ],
          supportedLocales: [
            const Locale('zh', 'CN'),
            const Locale('en', 'US'),
          ],
          initialRoute: RouterPath.splash,
          onGenerateRoute: prefix.RouterConfig.generateRoute,
          getPages: RouterPages.getPages,
        ),
        onWillPop: () async {
          if (lastTime != null) {
            if (DateTime.now().difference(lastTime!) >
                const Duration(seconds: 1)) {
              return true;
            }
          }
          lastTime = DateTime.now();

          BrnToast.show("再次点击退出程序", context);
          return false;
        },
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return utilInit;
  }
}
